package com.android.infantschool.ui.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.infantschool.R;

/**
 * Created by Administrator on 2018\2\25 0025.
 */
public class DialogVersion extends AlertDialog {

    private String TAG = getClass().getSimpleName();
    private Context mContext;
    private TextView tv_version;
    private TextView tv_uplode_content;
    private Button btn_enter;
    private Button btn_cencl;
    private volatile String new_version;
    private volatile String version_url;
    private volatile String version_content;
    private UpdateAppLister updateApp;

    /**
     * @param context        上下文
     * @param url            下载地址
     * @param version        服务器最新版本名
     * @param uplude_content 更新说明
     * @param updateApp      事件回调
     */
    public DialogVersion(Context context, String url, String version, String uplude_content, UpdateAppLister updateApp) {
        super(context);
        mContext = context;
        version_url = url;
        new_version = version;
        version_content = uplude_content;
        this.updateApp = updateApp;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_version_layout);
        setCanceledOnTouchOutside(false);
        init();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    private void init() {
        tv_version = (TextView) findViewById(R.id.tv_version);
        btn_cencl = (Button) findViewById(R.id.btn_cencl);
        btn_enter = (Button) findViewById(R.id.btn_enter);
        tv_version.setText("检测到最新版本v" + new_version);
        tv_uplode_content = (TextView) findViewById(R.id.tv_uplude_content);
        tv_uplode_content.setText(version_content);
        btn_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateApp.onSeccess(version_url);
                dismiss();
            }
        });

        btn_cencl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                updateApp.onDismiss();
            }
        });
    }

    public interface UpdateAppLister {
        void onSeccess(String updateUrl);

        void onDismiss();
    }

}